#!/usr/bin/bash

# SCRIPT VERSION
VER=29
VER_FILE="/etc/bazzite/flatpak_manager_version"
VER_RAN=$(cat $VER_FILE)
IMAGE_INFO="/usr/share/ublue-os/image-info.json"
IMAGE_NAME=$(jq -r '."image-name"' < $IMAGE_INFO)
BASE_IMAGE_NAME=$(jq -r '."base-image-name"' < $IMAGE_INFO)

# IMAGE IDENTIFIERS
KNOWN_IMAGE_NAME_FILE="/etc/bazzite/flatpak_manager_image_name"
KNOWN_IMAGE_NAME=$(cat $KNOWN_IMAGE_NAME_FILE)

# Run script if updated
if [[ -f $VER_FILE && $VER = $VER_RAN ]]; then
  if [[ -f $KNOWN_IMAGE_NAME_FILE ]]; then
    if [[ $IMAGE_NAME = $KNOWN_IMAGE_NAME ]]; then
      echo "Flatpak manager v$VER has already ran. Exiting..."
      exit 0
    fi
  fi
fi

# Ensure Flathub is enabled
flatpak remote-add --if-not-exists --system flathub /etc/flatpak/remotes.d/flathub.flatpakrepo
flatpak remote-modify --system --enable flathub

# Disable fedora flatpak remote
flatpak remote-modify --disable fedora || :
flatpak remote-modify --disable fedora-testing || :

# Filter out references from Flathub remote
if [[ -f /usr/share/ublue-os/flatpak-blocklist ]]; then
  flatpak remote-modify --system --filter=/usr/share/ublue-os/flatpak-blocklist flathub
fi

PROTONTRICKS_SETUP_FILE="/etc/bazzite/protontricks_installed"
if [[ ! -f "$PROTONTRICKS_SETUP_FILE" ]]; then
  if ! grep -q "com.github.Matoking.protontricks" <<< $(/usr/bin/flatpak list --app --columns=application); then
    flatpak install --system --assumeyes com.github.Matoking.protontricks
    touch "$PROTONTRICKS_SETUP_FILE"
  else
    touch "$PROTONTRICKS_SETUP_FILE"
  fi
fi

# Flatpak theming support
flatpak override \
  --filesystem=xdg-config/gtk-4.0:ro \
  --filesystem=xdg-config/gtk-3.0:ro

# XInput for Firefox
flatpak override \
  --system \
  --env=MOZ_USE_XINPUT2=1 \
  org.mozilla.firefox

# Fix printing on LibreOffice
flatpak override \
  --system \
  --socket=cups \
  --socket=session-bus \
  org.libreoffice.LibreOffice

# Allow MangoHUD config for Flatpaks
flatpak override \
  --filesystem=xdg-config/MangoHud:create \
  --filesystem=xdg-config/vkBasalt:create

# Fix permissions for XIV Launcher
flatpak override \
  --device=dri \
  dev.goats.xivlauncher

# Fix permissions for Protontricks
flatpak override \
  --filesystem=~/.local/share/Steam \
  --filesystem=/var/mnt \
  --filesystem=/run/media \
  com.github.Matoking.protontricks

# Allow smartphone's passkey for chrome browsers
for app in \
    com.brave.Browser \
    com.google.Chrome \
    io.github.ungoogled_software.ungoogled_chromium
do
    flatpak override --system-talk-name=org.bluez $app
done && unset -v app

# Params needed for HW accelerated video on Nvidia (Ignoring hybrid systems)
if [[ $IMAGE_NAME =~ "nvidia" ]] && [ $(grep -o "\-display" <<< $(lshw -C display) | wc -l) -le 1 ] && grep -q "vendor: NVIDIA Corporation" <<< $(lshw -C display); then
  flatpak override \
    --system \
    --filesystem=host-os \
    --env=LIBVA_DRIVER_NAME=nvidia \
    --env=LIBVA_DRIVERS_PATH=/run/host/usr/lib64/dri \
    --env=LIBVA_MESSAGING_LEVEL=1 \
    --env=MOZ_DISABLE_RDD_SANDBOX=1 \
    --env=NVD_BACKEND=direct \
    org.mozilla.firefox
else
  # Undo if user was previously using a Nvidia image and is no longer
  flatpak override \
    --system \
    --nofilesystem=host-os \
    --unset-env=LIBVA_DRIVER_NAME \
    --unset-env=LIBVA_DRIVERS_PATH \
    --unset-env=LIBVA_MESSAGING_LEVEL \
    --unset-env=MOZ_DISABLE_RDD_SANDBOX \
    --unset-env=NVD_BACKEND \
    org.mozilla.firefox
fi

# Set up Firefox default configuration
mkdir -p /var/lib/flatpak/extension/org.mozilla.firefox.systemconfig/x86_64/stable/defaults/pref
rm -f /var/lib/flatpak/extension/org.mozilla.firefox.systemconfig/x86_64/stable/defaults/pref/*bazzite*.js
/usr/bin/cp -rf /usr/share/ublue-os/firefox-config/* /var/lib/flatpak/extension/org.mozilla.firefox.systemconfig/x86_64/stable/defaults/pref/

mkdir -p /etc/bazzite
echo $VER > $VER_FILE
echo $IMAGE_NAME > $KNOWN_IMAGE_NAME_FILE
